Skip to content

Conversation

@ArpitaHanjagi
Copy link
Contributor

This program solves the Traveling Salesman Problem (TSP) using Dynamic Programming with Bitmasking.
It finds the minimum cost path to visit all cities exactly once and return to the starting city.

Uses a recursive function tsp(mask, pos) where mask tracks visited cities (in binary) and pos is the current city.

The DP table (dp) stores already computed states to avoid recomputation.

Base case: when all cities are visited, return cost to return to start.

Time Complexity: O(n² * 2ⁿ)

Output: Minimum traveling cost.

Copilot AI review requested due to automatic review settings October 20, 2025 18:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new implementation of the Traveling Salesman Problem (TSP) solver using Dynamic Programming with bitmasking. The solution efficiently computes the minimum cost to visit all cities exactly once and return to the starting city using memoization.

Key Changes:

  • Implements TSP with DP and bitmasking technique for optimal path finding
  • Uses recursive approach with memoization to avoid recomputation
  • Includes a working example with a 4-city cost matrix

# Try visiting all unvisited cities
for (city in 0:(n - 1)) {
if ((mask & (1 << city)) == 0) {
newAns <- cost[pos + 1, city + 1] + tsp(mask | (1 << city), city, cost, dp, n)
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'newAns' uses camelCase, which is inconsistent with R naming conventions. According to the repository guidelines, variables should use '.' or '_' to separate words (e.g., 'new_ans' or 'new.ans').

Copilot generated this review using guidance from repository custom instructions.
@siriak
Copy link
Member

siriak commented Oct 26, 2025

It's already implemented here

# each city exactly once and returns to the starting city. This implementation

@siriak siriak closed this Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants